package com.ghline.caloncabe.model;

import java.util.HashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

public class BitmapFont {
	
	private static final String TAG = BitmapFont.class.getSimpleName();
	private Bitmap bitmap;	// bitmap containing the character map/sheet

	// Map to associate a bitmap to each character
	private Map<Character, Bitmap> bmpFont = new HashMap<Character, Bitmap>(10);

	private int width;	// width in pixels of one character
	private int height;	// height in pixels of one character
	
	// the characters in the English alphabet
	private char[] numbers = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	
	public BitmapFont(Bitmap bitmap) {
		super();
		this.bitmap = bitmap;
		this.width = this.bitmap.getWidth() / 10;
		this.height = this.bitmap.getHeight();
		
		// row 3 for numbers
		Log.d(TAG, "Uppercases initialised");
		for (int i = 0; i < 10; i++) {
			bmpFont.put(numbers[i], Bitmap.createBitmap(this.bitmap,	0 + (i * width), 0, width, height));
		}
		Log.d(TAG, "Numbers initialised");
		
		// TODO - 4th row for punctuation
	}
	
	public Bitmap getBitmap() {
		return bitmap;
	}
	
	/**
	 * Draws the string onto the canvas at <code>x</code> and <code>y</code>
	 * @param text
	 */
	public void drawString(Canvas canvas, String text, int x, int y) {
		if (canvas == null) {
			Log.d(TAG, "Canvas is null");
		}
		for (int i = 0; i < text.length(); i++) {
			Character ch = text.charAt(i);
			if (bmpFont.get(ch) != null) {
				canvas.drawBitmap(bmpFont.get(ch), x + (i * width), y, null);
			}
		}
	}

}
